package com.nuoniu.sibanyun.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nuoniu.sibanyun.entity.SysArea;
import com.nuoniu.sibanyun.entity.dto.SysAreaDto;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 行政区划 Mapper 接口
 * </p>
 *
 * @author baiqiang
 * @since 2020-06-04
 */
@Component
@Mapper
public interface SysAreaMapper extends BaseMapper<SysArea> {

    /**
     * 根据地区code获取
     * @author 小啊刚
     * @date 2020/6/6
     */
    @Select("select * from sys_area where area_code = #{areaCode} and company_id = #{companyId}")
    SysArea getAreaByCode(Integer areaCode, Integer companyId);

    /**
     * 根据地区code获取 全节点名
     * @param areaCode
     * @param companyId
     * @return
     */
    @Select("SELECT tree_names FROM `sys_area` WHERE area_code = #{areaCode} AND company_id = #{companyId} LIMIT 1")
    String findAreaTreeNamesByCode(Integer areaCode, Integer companyId);

    /**
     * 修改父级
     * @author 小啊刚
     * @date 2020/6/8
     */
    @Update("update sys_area set tree_leaf = #{leaf} where area_code = #{parentCode} and company_id = #{companyId}")
    void updateLeafByCode(Integer parentCode, Integer companyId, Integer leaf);

    /**
     * 批量插入
     * @author 小啊刚
     * @date 2020/6/9
     */
    @Insert("INSERT INTO sys_area ( area_code, parent_code, parent_codes, company_id, company_name, tree_sort, tree_sorts, tree_leaf, tree_level, tree_names, area_name, area_type, status, is_update, create_by, update_by, remarks ) \n" +
            "SELECT area_code, parent_code, parent_codes, #{companyId}, #{companyName}, tree_sort, tree_sorts, tree_leaf, tree_level, tree_names, area_name, area_type, status, is_update, #{createBy}, #{updateBy}, remarks " +
            "FROM sys_area WHERE company_id = 1 AND area_code IN (${areaCodeStr})")
    void insertAll(SysAreaDto area);

    @Select("select area_code from sys_area where company_id = #{companyId}")
    List<Long> getAreaByCompany(Integer companyId, int isUpdate);

    @Update("update sys_area set tree_leaf = 0, is_update = 1 where area_code = #{parentCode} and company_id = #{companyId}")
    void updateAreaIsUpdate(Integer parentCode, Integer companyId);

    /**
     * 获取子级
     * @author 小啊刚
     * @date 2020/6/12
     */
    @Select("select * from sys_area where parent_code = #{parentCode} and company_id = #{companyId}")
    List<SysArea> getAreaByParentCode(Integer parentCode, Integer companyId);

    /**
     * 获取公司下所有地区
     * @author 小啊刚
     * @date 2020/7/6
     */
    @Select("select * from sys_area where company_id = #{companyId}")
    List<SysArea> getAreaMap(Integer companyId);

    /**
     * 根据treeNames查询地区
     * @author 小啊刚
     * @date 2020/7/6
     */
    @Select("select * from sys_area where tree_names = #{treeNames} and company_id = #{companyId}")
    SysArea getAreaByTreeNames(String treeNames, Integer companyId);

    @Select("select tree_names AS treeName,CONCAT(parent_codes,area_code) AS areaTreeCodes from sys_area where id = #{areaId}")
    HashMap<String,String> findAreaTreeNamesById(Integer areaId, Integer companyId);

    @Select("select id, area_code areaCode, area_name areaName,  parent_code parentCode, tree_leaf treeLeaf from sys_area where company_id = #{companyId}")
    List<Map> getTreeList(Integer companyId);
}
